import { fileURLToPath, URL } from 'node:url'

import { defineConfig, loadEnv } from 'vite'
import vue from '@vitejs/plugin-vue'
import vueDevTools from 'vite-plugin-vue-devtools'
export default defineConfig(({mode, command}) => {
  const env = loadEnv(mode, process.cwd());
  const { VITE_APP_ENV } = env;
  return {
    base: VITE_APP_ENV === 'production' ? '/' : '/',
    plugins: [
      vue({
        script: {
          defineModel: true
        }
      }),
      vueDevTools()
    ],
    resolve: {
      alias: {
        '@': fileURLToPath(new URL('./src', import.meta.url))
      },
      extensions: ['.mjs', '.js', '.ts', '.jsx', '.tsx', '.json', '.vue', '.css', '.scss']
    },
    css: {
      preprocessorOptions: {
        scss: {
          javascriptEnabled: true,
          // 全局引入主题色scss文件
          additionalData: "@import '@/assets/styles/theme.scss';"
        }
      }
    },
    server: {
      // port: 80,
      host: true,
      port: 8089,
      https: false,
      cors: true,
      open: true,
      proxy: {
        // '/api': {
        //   target: 'http://192.168.123.53:8090',
        //   changeOrigin: true,
        //   rewrite: (p) => p.replace(/^\/api/, '')
        // }
        // 本地后端
        // [env.VITE_APP_BASE_API]: {
        //   // target: 'http://192.168.102.166:8020',
        //   target: 'http://192.168.102.166:8011',
        //   changeOrigin: true,
        //   rewrite: (p) => p.replace(/^\/api/, '/api')
        // }
        [env.VITE_APP_BASE_API]: {
          // target: 'http://192.168.102.166:8020',
          target: 'http://192.168.102.166:8011',
          changeOrigin: true,
          rewrite: (p) => p.replace(/^\/api/, '')
        }
        // 线上
        // [env.VITE_APP_BASE_API]: {
        //   // target: 'http://192.168.123.53:8010',
        //   // target: 'http://192.168.102.166:8010',
        //   target: 'http://192.168.102.166:8011',
        //   // target: 'http://192.168.103.135:8091',
        //   changeOrigin: true,
        //   rewrite: (p) => p.replace(/^\/api/, '')
        // }
      }
    }  
  }
})